package com.example.dmweb.application;

import com.example.dmcommom.ResponseResult;
import com.example.dmcommom.SpringBeanHelper;
import com.example.dmdomain.ApplicationRepository;
import com.example.dmdomain.UserRepository;
import com.example.dmdomain.entity.ApplicationLog;
import com.example.dmdomain.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/application")
@CrossOrigin
@Api(value = "官网-应用", tags = {"应用"})
public class ApplicationController {


	@ApiOperation(value = "查询访问记录")
	@RequestMapping(value = "/getApplicationLog", method = {RequestMethod.GET})
	public ResponseResult<List<ApplicationLog>> getApplicationLog(HttpServletRequest request) {
		String userId = request.getHeader("Authorization");
		UserRepository userRepository = SpringBeanHelper.getBeanOrThrow(UserRepository.class);
		User user = userRepository.findUsersByUserID(userId);
		if (user == null) {
			return ResponseResult.error("用户不存在");
		}
		ApplicationRepository applicationRepository = SpringBeanHelper.getBeanOrThrow(ApplicationRepository.class);
		List<ApplicationLog> appList = applicationRepository.findAllByApplicationId_UserIdOrderByTimeDesc(userId);
		appList.sort((o1, o2) -> o2.getTime().compareTo(o1.getTime()));
		return ResponseResult.success(appList);
	}

	@ApiOperation(value = "添加访问记录")
	@RequestMapping(value = "/addApplicationLog/{applicationName}", method = {RequestMethod.PUT})
	public ResponseResult<List<ApplicationLog>> useApplication(@PathVariable String applicationName, HttpServletRequest request) {
		String userId = request.getHeader("Authorization");
		UserRepository userRepository = SpringBeanHelper.getBeanOrThrow(UserRepository.class);
		User user = userRepository.findUsersByUserID(userId);
		if (user == null) {
			return ResponseResult.error("用户不存在");
		}
		ApplicationRepository applicationRepository = SpringBeanHelper.getBeanOrThrow(ApplicationRepository.class);
		List<ApplicationLog> appList = applicationRepository.findAllByApplicationId_UserIdOrderByTimeDesc(userId);
		if (appList.isEmpty()) {
			ApplicationLog log = new ApplicationLog();
			log.setApplicationId(applicationName, userId);
			log.setTime(new Date());
			appList.add(log);
		} else {
			boolean exist = false;
			for (ApplicationLog applicationLog : appList) {
				if (applicationLog.getApplicationId().getApplicationName().equals(applicationName)) {
					applicationLog.setTime(new Date());
					exist = true;
				}
			}
			if (!exist) {
				ApplicationLog log = new ApplicationLog();
				log.setApplicationId(applicationName, userId);
				log.setTime(new Date());
				appList.add(log);
			}
		}
		appList.sort((o1, o2) -> o2.getTime().compareTo(o1.getTime()));
		applicationRepository.saveAll(appList);
		return ResponseResult.success(appList);
	}

	@ApiOperation(value = "删除访问记录")
	@RequestMapping(value = "/delApplicationLog/{applicationName}", method = {RequestMethod.DELETE})
	public ResponseResult<List<ApplicationLog>> delAppLog(@PathVariable String applicationName, HttpServletRequest request) {
		String userId = request.getHeader("Authorization");
		UserRepository userRepository = SpringBeanHelper.getBeanOrThrow(UserRepository.class);
		User user = userRepository.findUsersByUserID(userId);
		if (user == null) {
			return ResponseResult.error("用户不存在");
		}
		ApplicationRepository applicationRepository = SpringBeanHelper.getBeanOrThrow(ApplicationRepository.class);
		List<ApplicationLog> appList = applicationRepository.findAllByApplicationId_UserIdOrderByTimeDesc(userId);
		if (appList.isEmpty()) {
			return ResponseResult.error("应用记录不存在");
		}
		for (ApplicationLog log : appList) {
			if (log.getApplicationName().equals(applicationName)){
				applicationRepository.delete(log);
				appList.remove(log);
				break;
			}
		}
		appList.sort((o1, o2) -> o2.getTime().compareTo(o1.getTime()));
		return ResponseResult.success(appList);
	}
}
